
# MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MKFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
TARGET_PLATFORM=$(shell uname)



SRC_ROOT=$(MKFILE_DIR)/
OUTPUT_DIR=$(MKFILE_DIR)/bin


#functions
get_files =$(foreach dir,$(subst $(DIR_DIVIDER),$(MK_DIR_DIVIDER),$(1)),$(wildcard $(dir)$(MK_DIR_DIVIDER)$(2)))




COMMON_SRC_DIRS += $(MKFILE_DIR)/src/ 
COMMON_SRC_DIRS += $(MKFILE_DIR)/src/rtp/ 
COMMON_C_SRC += $(call get_files,$(COMMON_SRC_DIRS),*.c)
COMMON_CPP_SRC += $(call get_files,$(COMMON_SRC_DIRS),*.cpp)
HEAD_DIRS += $(COMMON_SRC_DIRS)
COMMON_C_OBJ += $(foreach src, $(COMMON_C_SRC), $(addsuffix .o, $(basename $(src))))
COMMON_CPP_OBJ += $(foreach src, $(COMMON_CPP_SRC), $(addsuffix .o, $(basename $(src))))
ALL_C_OBJ += $(COMMON_C_OBJ)
ALL_CPP_OBJ += $(COMMON_CPP_OBJ)
COMMON_OBJ += $(COMMON_C_OBJ)
COMMON_OBJ += $(COMMON_CPP_OBJ)

WEBRTC_DIR := $(MKFILE_DIR)/third/webrtc
HEAD_DIRS += $(WEBRTC_DIR)
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/modules/audio_coding/include
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/modules/include
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/common_video
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/system_wrappers/include
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/common_audio/vad/include
# HEAD_DIRS += $(WEBRTC_DIR)/third_party/opus/src/celt
# HEAD_DIRS += $(WEBRTC_DIR)/third_party/opus/src/include
# HEAD_DIRS += $(WEBRTC_DIR)/third_party/opus/src/src
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/common_audio/signal_processing/include
# HEAD_DIRS += $(WEBRTC_DIR)/third_party/jsoncpp/source/include 
# HEAD_DIRS += $(WEBRTC_DIR)/webrtc/modules/rtp_rtcp/include 
LIB_PATHS += -L$(WEBRTC_DIR)/lib
MY_LD_FLAGS += -lwebrtc

RTC_SWITCH_TARGET:=$(OUTPUT_DIR)/rtc-xswitch
TARGETS += $(RTC_SWITCH_TARGET)
RTC_SWITCH_TARGET_OBJ = $(MKFILE_DIR)/app/main_rtc_xswitch.o 
ALL_CPP_OBJ += $(RTC_SWITCH_TARGET_OBJ)

UDP_RELAY_TARGET:=$(OUTPUT_DIR)/udp-relay
TARGETS += $(UDP_RELAY_TARGET)
UDP_RELAY_TARGET_OBJ = $(MKFILE_DIR)/app/main_udp_relay.o 
ALL_CPP_OBJ += $(UDP_RELAY_TARGET_OBJ)


# ifdef CONFIG_SIGNAL_APP	
SIGNAL_APP_TARGET:=$(OUTPUT_DIR)/rtc-signal
TARGETS += $(SIGNAL_APP_TARGET)
SIGNAL_APP_TARGET_OBJ = $(MKFILE_DIR)/app/main_rtc_signal.o 
ALL_CPP_OBJ += $(SIGNAL_APP_TARGET_OBJ)

# DEP_PKG_NAMES += libwebsockets
# DEP_PKG_NAMES += openssl
# PKG_ENV +=  PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig
# endif




MY_FLAGS += -g -Wall
# for MacOS
MY_FLAGS += -fno-pie 


# LIBNICE_PATH_ :=$(MKFILE_DIR)/third/libnice
# # LIBNICE_PATH_ :=/Users/simon/easemob/src/study/libnice/nice
# # ifdef LIBNICE_PATH	
# # LIBNICE_PATH_ := $(LIBNICE_PATH)
# # endif
# LIB_PATHS += -L$(LIBNICE_PATH_)/lib
# HEAD_DIRS += $(LIBNICE_PATH_)/include

LIB_PATHS += -L$(MKFILE_DIR)/stage/lib/
HEAD_DIRS += $(MKFILE_DIR)/stage/include/nice/


LIBSRTP_PATH_ :=/usr/local/Cellar/srtp/2.0.0
ifdef LIBSRTP_PATH	
LIBSRTP_PATH_ := $(LIBSRTP_PATH)
endif
LIB_PATHS += -L$(LIBSRTP_PATH_)/lib
HEAD_DIRS += $(LIBSRTP_PATH_)/include






# HEAD_DIRS += /usr/local/include/
INCLUDE_DIRS += $(addprefix -I,$(HEAD_DIRS))


#CC=gcc
CC=g++

MV = mv
RM = rm -fr
STRIP = strip


# ALL_C_OBJ += $(foreach src, $(ALL_C_SRC), $(addsuffix .o, $(basename $(src))))
# ALL_CPP_OBJ += $(foreach cpp, $(ALL_CPP_SRC), $(addsuffix .o, $(basename $(cpp))))


# -fno-strict-aliasing remove the warning "dereferencing type-punned pointer will break strict-aliasing rules "
# 



#CFLAGS = -fPIC -fno-strict-aliasing  -Wall -O2 $(INCLUDE_DIRS) 
CFLAGS+= $(INCLUDE_DIRS) $(MACROS)  $(MY_FLAGS) 
#CFLAGS += -fstack-protector 
#CFLAGS += -std=c++11
#CFLAGS += -std=c++0x


CPPFLAGS+=$(INCLUDE_DIRS) $(MACROS)  $(MY_FLAGS) 


INCLUDE_DIRS += -I/usr/local/opt/openssl/include
LIB_PATHS += -L/usr/local/opt/openssl/lib


LIB_PATHS+= -L/usr/local/lib 
MY_LD_FLAGS += $(LIB_PATHS)
MY_LD_FLAGS += -lstdc++ 
MY_LD_FLAGS += -lm
# MY_LD_FLAGS += -lboost_system
# MY_LD_FLAGS += -lhiredis
MY_LD_FLAGS += -lpthread
MY_LD_FLAGS += -lcrypto
# MY_LD_FLAGS += -lsocket -lagent -lsrtp2
MY_LD_FLAGS += -lnice -lsrtp2

# MY_LD_FLAGS += -llog4cplus -lcppunit 

# MY_LD_FLAGS += -pthread
# MY_LD_FLAGS += -lrt 

MY_LD_FLAGS += -g -fno-pie

DEP_PKG_NAMES += libevent log4cplus glib-2.0 gobject-2.0 gio-2.0 libssl 
# DEP_PKG_NAMES += nice
# DEP_PKG_NAMES += speex
DEP_PKG_NAMES += opus





ifneq '$(TARGET_PLATFORM)' 'Darwin'
#MY_FLAGS+= -std=c++11
LIB_PATHS += -L/usr/lib64 

CFLAGS += $(shell "pkg-config uuid --cflags")
CPPFLAGS += $(shell pkg-config uuid --cflags)
MY_LD_FLAGS += $(shell pkg-config uuid --libs)
CPPFLAGS += -std=c++11
DEP_PKG_NAMES += libsoup-2.4
else
#MY_FLAGS+= -std=c++0x
#MY_FLAGS+= -Wc++11-extensions
CPPFLAGS += -std=c++11
MY_LD_FLAGS += -framework CoreServices

CPPFLAGS += -I/usr/local/Cellar/libsoup/2.52.2_1/include/libsoup-2.4
MY_LD_FLAGS += -L/usr/local/Cellar/libsoup/2.52.2_1/lib -lsoup-2.4
endif

CFLAGS += $(shell "$(PKG_ENV) pkg-config $(DEP_PKG_NAMES) --cflags")
CPPFLAGS += $(shell $(PKG_ENV) pkg-config $(DEP_PKG_NAMES) --cflags)
MY_LD_FLAGS += $(shell $(PKG_ENV) pkg-config $(DEP_PKG_NAMES) --libs)

# git log --pretty=oneline
GIT_COMMIT_VER := $(shell git log --format=format:%H -n 1)
CFLAGS += -DAPP_VER=$(GIT_COMMIT_VER)
CPPFLAGS += -DAPP_VER=$(GIT_COMMIT_VER)


all: $(TARGETS)
	@echo build success


$(RTC_SWITCH_TARGET): $(RTC_SWITCH_TARGET_OBJ) $(COMMON_OBJ) $(COMMON_H) $(OUTPUT_DIR) 
	$(CC)  $(RTC_SWITCH_TARGET_OBJ) $(COMMON_OBJ) $(MY_LD_FLAGS) -o $@

$(UDP_RELAY_TARGET): $(UDP_RELAY_TARGET_OBJ) $(COMMON_OBJ) $(COMMON_H) $(OUTPUT_DIR) 
	$(CC)  $(UDP_RELAY_TARGET_OBJ) $(COMMON_OBJ) $(MY_LD_FLAGS) -o $@
	

$(SIGNAL_APP_TARGET): $(SIGNAL_APP_TARGET_OBJ) $(COMMON_OBJ) $(COMMON_H) $(OUTPUT_DIR) 
	$(CC)  $(SIGNAL_APP_TARGET_OBJ) $(COMMON_OBJ) $(MY_LD_FLAGS) -o $@

$(OUTPUT_DIR):
	mkdir -p $@


bd:$(C_OBJ)
	echo $<

$(ALL_C_OBJ):%.o:%.c
	$(CC) -c $(CFLAGS) $< -o $@

$(ALL_CPP_OBJ):%.o:%.cpp
	$(CC) -c $(CPPFLAGS) $< -o $@

$(ALL_CC_OBJ):%.o:%.cc
	$(CC) -c $(CPPFLAGS) $< -o $@

	
.PHONY: clean print_info
clean:
	$(RM) $(TARGETS)
	$(RM) $(ALL_C_OBJ)
	$(RM) $(ALL_CPP_OBJ)
	$(RM) $(ALL_CC_OBJ)
#	$(RM) ./bin/mserver.dSYM

print:
	@echo TARGET_PLATFORM=$(TARGET_PLATFORM)
	@echo ----------------------
	@echo CC=$(CC)
	@echo ----------------------
	@echo CFLAGS=$(CFLAGS)
	@echo ----------------------
	@echo CPPFLAGS=$(CPPFLAGS)
	@echo ----------------------
	@echo C_SRC=$(C_SRC)
	@echo ----------------------
	@echo CPP_SRC=$(CPP_SRC)
	@echo ----------------------
	@echo C_OBJ=$(C_OBJ)
	@echo ----------------------
	@echo CPP_OBJ=$(CPP_OBJ)
	@echo ----------------------
	@echo ALL_C_SRC=[$(ALL_C_SRC)]
	@echo ----------------------
	@echo ALL_CPP_SRC=[$(ALL_CPP_SRC)]
	@echo GIT_COMMIT_VER=$(GIT_COMMIT_VER)





